﻿using In66.Net.Repository.EFCoreData.Transaction;

namespace In66.Net.Repository.EFCoreData.MySql
{
    public class MySqlUnitOfWork<TDbContext> : UnitOfWork<TDbContext>
    where TDbContext : MySqlDbContext
    {
        private ICapPublisher? _publisher;

        public MySqlUnitOfWork(
            TDbContext context
            , ICapPublisher? publisher = null)
            : base(context)
        {
            _publisher = publisher;
        }

        protected override IDbContextTransaction GetDbContextTransaction(
            IsolationLevel isolationLevel = IsolationLevel.ReadCommitted
            , bool distributed = false)
        {
            if (distributed)
            {
                if (_publisher is null)
                {
                    throw new ArgumentException("CapPublisher is null");
                }
                else
                {
                    return _DbContext.Database.BeginTransaction(_publisher, false);
                }
            }
            else
            {
                return _DbContext.Database.BeginTransaction(isolationLevel);
            }
        }
    }
}
